package com.example.administrator.waitan.main;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.util.Log;


import com.example.administrator.waitan.R;
import com.example.administrator.waitan.base.BaseActivity;
import com.example.administrator.waitan.find.FindFragment;
import com.example.administrator.waitan.homepage.HomePagerFragment;
import com.example.administrator.waitan.hot.HotFragment;
import com.example.administrator.waitan.mine.MineFragment;

import java.util.ArrayList;

import de.greenrobot.event.EventBus;
import de.greenrobot.event.Subscribe;

public class MainActivity extends BaseActivity {
    private ViewPager mViewPager;
    private ArrayList<Fragment> data;
    private TabLayout mTablayout;
    private MainViewPagerAdapter mAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //订阅事件
        EventBus.getDefault().register(this);
        mViewPager = (ViewPager) findViewById(R.id.vp_main);
        data = new ArrayList<>();
        data.add(new FindFragment());
        data.add(new HotFragment());
        data.add(new HomePagerFragment());
        data.add(new MineFragment());
        mAdapter = new MainViewPagerAdapter(getSupportFragmentManager(),this,data);
        mTablayout = (TabLayout) findViewById(R.id.tabLayout_main);
//        mViewPager.setOffscreenPageLimit(2);
        mViewPager.setAdapter(mAdapter);
        mTablayout.setupWithViewPager(mViewPager);

        for(int i = 0;i < mTablayout.getTabCount();i++){//判断tablayout有几个菜单数量
            TabLayout.Tab tab = mTablayout.getTabAt(i);
            if (tab != null){
                tab.setCustomView(mAdapter.getTabView(i));//将adapter设置好的获取图片和文字的方法设置到视图中
            }
        }
        mViewPager.setCurrentItem(0);
    }



    //event方法  发送事件的方法
    @Subscribe//对方法进行标记  可以更改方法名
    public void onEvent(String a){
        Log.d("--",a);
        mViewPager.setCurrentItem(1);
    }
}
